package com.seqb.manage.task.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.Page;
import com.seqb.annotation.Log;
import com.seqb.common.Query;
import com.seqb.common.ResultVo;
import com.seqb.common.controller.BaseController;
import com.seqb.common.utils.RedisUtils;
import com.seqb.manage.task.domain.TaskDO;
import com.seqb.manage.task.service.TaskService;
import com.seqb.utils.PageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author lilinshen
 * @title 定时任务表
 * @description 请填写相关描述
 * @date 2019/11/15
 */
@Controller
@RequestMapping("/manage/task/task")
public class TaskController extends BaseController {
    @Autowired
    private TaskService taskService;
    @Autowired
    private RedisUtils redisUtils;

    @GetMapping()
    @RequiresPermissions("manage:task:task:list")
    @Log("定时任务表列表")
    String task() {
        return "manage/task/task.list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("manage:task:task:list")
    @Log("定时任务表列表")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<TaskDO> page = taskService.list(query);
        return new PageUtils(page.getResult(), page.getTotal());
    }

    @GetMapping("/add")
    @RequiresPermissions("manage:task:task:add")
    @Log("定时任务表添加")
    String add() {
        return "manage/task/task.add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("manage:task:task:edit")
    @Log("定时任务表编辑")
    String edit(@PathVariable("id") Long id, Model model) {
        TaskDO task = taskService.get(id);
        model.addAttribute("task", task);
        return "manage/task/task.edit";
    }

    /**
     * 添加保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("manage:task:task:add")
    @Log("定时任务表添加保存")
    public ResultVo save(TaskDO task) {
        if (taskService.save(task) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 编辑保存
     */
    @ResponseBody
    @RequestMapping("/update")
    @Log("定时任务表编辑保存")
    @RequiresPermissions("manage:task:task:edit")
    public ResultVo update(TaskDO task) {
        taskService.update(task);
        return ResultVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("manage:task:task:remove")
    @Log("定时任务表删除")
    public ResultVo remove(Long id) {
        if (taskService.remove(id) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("manage:task:task:batchRemove")
    @Log("定时任务表批量删除")
    public ResultVo remove(@RequestParam("ids[]") Long[] ids) {
        taskService.batchRemove(ids);
        return ResultVo.ok();
    }

    @PostMapping(value = "/changeJobStatus")
    @ResponseBody
    public R changeJobStatus(Long id, String state) {
        String label = "停止";
        if ("1".equals(state)) {
            label = "启动";
        } else {
            label = "停止";
        }
        try {
            taskService.changeStatus(id, state);
            return R.ok("任务" + label + "成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok("任务" + label + "失败");
    }

    @GetMapping("/params")
    @Log("定时任务表编辑")
    String params(Model model) {
        model.addAttribute("taskParams", redisUtils.get("taskParams"));
        return "manage/task/task.params";
    }

    @PostMapping(value = "/params/save")
    @ResponseBody
    public R paramsSave(String taskParams) {
        redisUtils.set("taskParams", taskParams);
        return R.ok("操作成功");
    }
}
